﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using DBUtility;
using MySql.Data.MySqlClient;

namespace DAL
{
    public class DRole
    {
        #region  Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperMySQL.GetMaxID("id", "trole");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string roleName)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from trole");
            strSql.Append(" where roleName=@roleName");
            MySqlParameter[] parameters = {
					new MySqlParameter("@roleName", MySqlDbType.String)
          };
            parameters[0].Value = roleName;

            return DbHelperMySQL.Exists(strSql.ToString(), parameters);
        }

        public string[] GetRoleInfoByUserName(string username)
        {
            //SELECT Permission,RoleName FROM trole t where id=(select Roleid from tuser where
             //UserName = 'admin'); 
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT Permission,RoleName FROM trole t ");
            strSql.Append("where id=(select Roleid from tuser where UserName = @UserName)");            
            MySqlParameter[] parameters = {
					new MySqlParameter("@@UserName", MySqlDbType.String)
          };
            parameters[0].Value = username;
            DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);
            if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
                return null;
            else
            {
                string rolename = ds.Tables[0].Rows[0]["RoleName"].ToString();
                string permission = ds.Tables[0].Rows[0]["Permission"].ToString();
                string[] roleInfo = new string[]{rolename,permission};
                return roleInfo;
            }
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Model.RoleInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into trole(");
            strSql.Append("RoleName,Permission,Remark)");
            strSql.Append(" values (");
            strSql.Append("@RoleName,@Permission,@Remark)");
            MySqlParameter[] parameters = {
					new MySqlParameter("@RoleName", MySqlDbType.VarChar,20),
					new MySqlParameter("@Permission", MySqlDbType.Text),
					new MySqlParameter("@Remark", MySqlDbType.VarChar,45)};
            parameters[0].Value = model.RoleName;
            parameters[1].Value = model.Permission;
            parameters[2].Value = model.Remark;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(int id,string name,string permission,string remark)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update trole set ");
            strSql.Append("RoleName=@RoleName,");
            strSql.Append("Permission=@Permission,");
            strSql.Append("Remark=@Remark");
            strSql.Append(" where id=@id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@RoleName", MySqlDbType.VarChar,20),
					new MySqlParameter("@Permission", MySqlDbType.Text),
					new MySqlParameter("@Remark", MySqlDbType.VarChar,45),
					new MySqlParameter("@id", MySqlDbType.Int32,10)};
            parameters[0].Value = name;
            parameters[1].Value = permission;
            parameters[2].Value = remark;
            parameters[3].Value = id;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from trole ");
            strSql.Append(" where id=@id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int32)
};
            parameters[0].Value = id;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from trole ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.RoleInfo GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,RoleName,Permission,Remark from trole ");
            strSql.Append(" where id=@id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int32)
};
            parameters[0].Value = id;

            Model.RoleInfo model = new Model.RoleInfo();
            DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"] != null && ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["RoleName"] != null && ds.Tables[0].Rows[0]["RoleName"].ToString() != "")
                {
                    model.RoleName = ds.Tables[0].Rows[0]["RoleName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Permission"] != null && ds.Tables[0].Rows[0]["Permission"].ToString() != "")
                {
                    model.Permission = ds.Tables[0].Rows[0]["Permission"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Remark"] != null && ds.Tables[0].Rows[0]["Remark"].ToString() != "")
                {
                    model.Remark = ds.Tables[0].Rows[0]["Remark"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,RoleName,Permission,Remark ");
            strSql.Append(" FROM trole ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperMySQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM trole ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.id desc");
            }
            strSql.Append(")AS Row, T.*  from trole T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperMySQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            MySqlParameter[] parameters = {
                    new MySqlParameter("@tblName", MySqlDbType.VarChar, 255),
                    new MySqlParameter("@fldName", MySqlDbType.VarChar, 255),
                    new MySqlParameter("@PageSize", MySqlDbType.Int32),
                    new MySqlParameter("@PageIndex", MySqlDbType.Int32),
                    new MySqlParameter("@IsReCount", MySqlDbType.Bit),
                    new MySqlParameter("@OrderType", MySqlDbType.Bit),
                    new MySqlParameter("@strWhere", MySqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "trole";
            parameters[1].Value = "id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperMySQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}
